﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Buy2Site.MasterPages;
using Buy2Site.UserControls;
using Buy2Site.Enums;
using Buy2Site.Classes;
using System.IO;

namespace Buy2Site.Pages
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((MySiteMaster)Master).PageTitle = "Home";
            // ask if for user session
            //if (Session["User"] != null)
            //{
                //Page.Title = "Home";
                ((MySiteMaster)Master).HideLink(LinksEnum.HomeLink);
                if (Session["User"] != null)
                {
                    ((MySiteMaster)Master).LogInVisible = false;
                    ((MySiteMaster)Master).HelloUserVisible = true;
                }
                else if (Session["User"] == null)
                {
                    ((MySiteMaster)Master).LogInVisible = true;
                    ((MySiteMaster)Master).HelloUserVisible = false;
                }
                if(rdbSortByTitle.Checked==true)
                    SortProducts(SortEnums.ByTitle);
                if (rdbSortByDate.Checked == true)
                    SortProducts(SortEnums.ByDate);
            //}
        }

        protected void rdbSortByDate_CheckedChanged(object sender, EventArgs e)
        {
            SortProducts(SortEnums.ByDate);
        }

        protected void rdbSortByTitle_CheckedChanged(object sender, EventArgs e)
        {
            SortProducts(SortEnums.ByTitle);
        }

        private class CaseInsensitiveComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
            }
        }
        private void SortProducts(SortEnums sortEnum)
        {
            using (Buy2SiteDBEntities ent = new Buy2SiteDBEntities())
            {
                int indexOfProducts = 0;

                var products = (dynamic)null;

                if (sortEnum == SortEnums.ByDate)
                {
                    products = from p in ent.Product
                               orderby p.Date ascending
                               select p;                    
                }
                else if (sortEnum == SortEnums.ByTitle)
                {
                    //products = ent.Product.OrderBy(a => a.Title);
                    products = from p in ent.Product
                                   orderby p.Title ascending
                                   select p;
                }
                // clear content place holder
                this.productsPlaceHolder.Controls.Clear();
                //this.productsPlaceHolder.Controls.Add(new LiteralControl("<br />"));
                foreach (var item in products)
                {
                    int i = 1;

                    ProductControl productControl = (ProductControl)LoadControl("~/UserControls/ProductControl.ascx");
                    ((ProductControl)productControl).Title = item.Title;
                    ((ProductControl)productControl).Price = item.Price;
                    ((ProductControl)productControl).Date = item.Date;
                    ((ProductControl)productControl).ShortDescription = item.ShortDescription;
                    //((ProductControl)productControl).LongDescription = item.LongDescription;

                    // fix the id - fixed
                    ((ProductControl)productControl).Image1 =
                        "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + item.ID + "&pictureNumerID=" + i;
                    //((ProductControl)productControl).Image2 =
                    //    "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + item.ID + "&pictureNumerID=" + (++i);
                    //((ProductControl)productControl).Image3 =
                    //    "~/Handlers/HandlerAddProductImageToWebSite.ashx?productID=" + item.ID + "&pictureNumerID=" + (++i);
                    productControl.ProductID = item.ID;
                    // if item was bougth it will not be displayed
                    ProductClass product = new ProductClass()
                    {
                        Title = item.Title,
                        Price = Convert.ToDouble(item.Price),
                        Date = item.Date,
                        ShortDescription = item.ShortDescription,
                        LongDescription = item.LongDescription,
                        Picture1 = item.Picture1,
                        Picture2 = item.Picture2,
                        Picture3 = item.Picture3
                    };
                    
                    // TODO: fix bug
                    if (((ProductClass)Application["ListOfAllQuantetiesItems"]) != null &&
                        !((ProductClass)Application["ListOfAllQuantetiesItems"]).ListOfAllQuantetiesItems.Contains(product))
                    {
                        this.productsPlaceHolder.Controls.Add(productControl);
                    }
                    // TODO: fix - only 3 products per line

                    if (indexOfProducts % 3 == 0 && indexOfProducts>1)
                    {
                        this.productsPlaceHolder.Controls.Add(new LiteralControl("<br />"));                      
                        
                        //indexOfProducts = 0;
                    }
                    //this.productsPlaceHolder.Controls.Add(new LiteralControl("<br />"));
                    this.productsPlaceHolder.Controls.Add(productControl);
                    indexOfProducts++;
                    //this.productsPlaceHolder.Controls.AddAt(indexOfProducts, productControl);

                }
            }
        }
        
    }
}